home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000007.txt < prev    next >
Text File  |  2013-04-03  |  7KB  |  231 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. cr.define('bmm', function() {
  6.   const Promise = cr.Promise;
  7.  
  8.   /**
  9.    * Whether a node contains another node.
  10.    * @param {!BookmarkTreeNode} parent
  11.    * @param {!BookmarkTreeNode} descendant
  12.    * @return {boolean} Whether the parent contains the descendant.
  13.    */
  14.   function contains(parent, descendant) {
  15.     if (descendant.parentId == parent.id)
  16.       return true;
  17.     // the bmm.treeLookup contains all folders
  18.     var parentTreeItem = bmm.treeLookup[descendant.parentId];
  19.     if (!parentTreeItem || !parentTreeItem.bookmarkNode)
  20.       return false;
  21.     return this.contains(parent, parentTreeItem.bookmarkNode);
  22.   }
  23.  
  24.   /**
  25.    * @param {!BookmarkTreeNode} node The node to test.
  26.    * @return {boolean} Whether a bookmark node is a folder.
  27.    */
  28.   function isFolder(node) {
  29.     return !('url' in node);
  30.   }
  31.  
  32.   var loadingPromises = {};
  33.  
  34.   /**
  35.    * Loads a subtree of the bookmark tree and returns a {@code cr.Promise} that
  36.    * will be fulfilled when done. This reuses multiple loads so that we do not
  37.    * load the same subtree more than once at the same time.
  38.    * @return {!cr.Promise} The future promise for the load.
  39.    */
  40.   function loadSubtree(id) {
  41.     var p = new Promise;
  42.     if (!(id in loadingPromises)) {
  43.       loadingPromises[id] = new Promise;
  44.       chrome.bookmarkManagerPrivate.getSubtree(id, false, function(nodes) {
  45.         loadingPromises[id].value = nodes && nodes[0];
  46.         delete loadingPromises[id];
  47.       });
  48.     }
  49.     loadingPromises[id].addListener(function(n) {
  50.       p.value = n;
  51.     });
  52.     return p;
  53.   }
  54.  
  55.   /**
  56.    * Loads the entire bookmark tree and returns a {@code cr.Promise} that will
  57.    * be fulfilled when done. This reuses multiple loads so that we do not load
  58.    * the same tree more than once at the same time.
  59.    * @return {!cr.Promise} The future promise for the load.
  60.    */
  61.   function loadTree() {
  62.     return loadSubtree('');
  63.   }
  64.  
  65.   var bookmarkCache = {
  66.     /**
  67.      * Removes the cached item from both the list and tree lookups.
  68.      */
  69.     remove: function(id) {
  70.       var treeItem = bmm.treeLookup[id];
  71.       if (treeItem) {
  72.         var items = treeItem.items; // is an HTMLCollection
  73.         for (var i = 0; i < items.length; ++i) {
  74.           var item = items[i];
  75.           var bookmarkNode = item.bookmarkNode;
  76.           delete bmm.treeLookup[bookmarkNode.id];
  77.         }
  78.         delete bmm.treeLookup[id];
  79.       }
  80.     },
  81.  
  82.     /**
  83.      * Updates the underlying bookmark node for the tree items and list items by
  84.      * querying the bookmark backend.
  85.      * @param {string} id The id of the node to update the children for.
  86.      * @param {Function=} opt_f A funciton to call when done.
  87.      */
  88.     updateChildren: function(id, opt_f) {
  89.       function updateItem(bookmarkNode) {
  90.         var treeItem = bmm.treeLookup[bookmarkNode.id];
  91.         if (treeItem) {
  92.           treeItem.bookmarkNode = bookmarkNode;
  93.         }
  94.       }
  95.  
  96.       chrome.bookmarks.getChildren(id, function(children) {
  97.         if (children)
  98.           children.forEach(updateItem);
  99.  
  100.         if (opt_f)
  101.           opt_f(children);
  102.       });
  103.     }
  104.   };
  105.  
  106.   /**
  107.    * Called when the title of a bookmark changes.
  108.    * @param {string} id
  109.    * @param {!Object} changeInfo
  110.    */
  111.   function handleBookmarkChanged(id, changeInfo) {
  112.     if (bmm.tree)
  113.       bmm.tree.handleBookmarkChanged(id, changeInfo);
  114.     if (bmm.list)
  115.       bmm.list.handleBookmarkChanged(id, changeInfo);
  116.   }
  117.  
  118.   /**
  119.    * Callback for when the user reorders by title.
  120.    * @param {string} id The id of the bookmark folder that was reordered.
  121.    * @param {!Object} reorderInfo The information about how the items where
  122.    *     reordered.
  123.    */
  124.   function handleChildrenReordered(id, reorderInfo) {
  125.     if (bmm.tree)
  126.       bmm.tree.handleChildrenReordered(id, reorderInfo);
  127.     if (bmm.list)
  128.       bmm.list.handleChildrenReordered(id, reorderInfo);
  129.     bookmarkCache.updateChildren(id);
  130.   }
  131.  
  132.   /**
  133.    * Callback for when a bookmark node is created.
  134.    * @param {string} id The id of the newly created bookmark node.
  135.    * @param {!Object} bookmarkNode The new bookmark node.
  136.    */
  137.   function handleCreated(id, bookmarkNode) {
  138.     if (bmm.list)
  139.       bmm.list.handleCreated(id, bookmarkNode);
  140.     if (bmm.tree)
  141.       bmm.tree.handleCreated(id, bookmarkNode);
  142.     bookmarkCache.updateChildren(bookmarkNode.parentId);
  143.   }
  144.  
  145.   /**
  146.    * Callback for when a bookmark node is moved.
  147.    * @param {string} id The id of the moved bookmark node.
  148.    * @param {!Object} moveInfo The information about move.
  149.    */
  150.   function handleMoved(id, moveInfo) {
  151.     if (bmm.list)
  152.       bmm.list.handleMoved(id, moveInfo);
  153.     if (bmm.tree)
  154.       bmm.tree.handleMoved(id, moveInfo);
  155.  
  156.     bookmarkCache.updateChildren(moveInfo.parentId);
  157.     if (moveInfo.parentId != moveInfo.oldParentId)
  158.       bookmarkCache.updateChildren(moveInfo.oldParentId);
  159.   }
  160.  
  161.   /**
  162.    * Callback for when a bookmark node is removed.
  163.    * @param {string} id The id of the removed bookmark node.
  164.    * @param {!Object} bookmarkNode The information about removed.
  165.    */
  166.   function handleRemoved(id, removeInfo) {
  167.     if (bmm.list)
  168.       bmm.list.handleRemoved(id, removeInfo);
  169.     if (bmm.tree)
  170.       bmm.tree.handleRemoved(id, removeInfo);
  171.  
  172.     bookmarkCache.updateChildren(removeInfo.parentId);
  173.     bookmarkCache.remove(id);
  174.   }
  175.  
  176.   /**
  177.    * Callback for when importing bookmark is started.
  178.    */
  179.   function handleImportBegan() {
  180.     chrome.bookmarks.onCreated.removeListener(handleCreated);
  181.     chrome.bookmarks.onChanged.removeListener(handleBookmarkChanged);
  182.   }
  183.  
  184.   /**
  185.    * Callback for when importing bookmark node is finished.
  186.    */
  187.   function handleImportEnded() {
  188.     // When importing is done we reload the tree and the list.
  189.  
  190.     function f() {
  191.       bmm.tree.removeEventListener('load', f);
  192.  
  193.       chrome.bookmarks.onCreated.addListener(handleCreated);
  194.       chrome.bookmarks.onChanged.addListener(handleBookmarkChanged);
  195.  
  196.       if (!bmm.list)
  197.         return;
  198.  
  199.       // TODO(estade): this should navigate to the newly imported folder, which
  200.       // may be the bookmark bar if there were no previous bookmarks.
  201.       bmm.list.reload();
  202.     }
  203.  
  204.     if (bmm.tree) {
  205.       bmm.tree.addEventListener('load', f);
  206.       bmm.tree.reload();
  207.     }
  208.   }
  209.  
  210.   /**
  211.    * Adds the listeners for the bookmark model change events.
  212.    */
  213.   function addBookmarkModelListeners() {
  214.     chrome.bookmarks.onChanged.addListener(handleBookmarkChanged);
  215.     chrome.bookmarks.onChildrenReordered.addListener(handleChildrenReordered);
  216.     chrome.bookmarks.onCreated.addListener(handleCreated);
  217.     chrome.bookmarks.onMoved.addListener(handleMoved);
  218.     chrome.bookmarks.onRemoved.addListener(handleRemoved);
  219.     chrome.bookmarks.onImportBegan.addListener(handleImportBegan);
  220.     chrome.bookmarks.onImportEnded.addListener(handleImportEnded);
  221.   };
  222.  
  223.   return {
  224.     contains: contains,
  225.     isFolder: isFolder,
  226.     loadSubtree: loadSubtree,
  227.     loadTree: loadTree,
  228.     addBookmarkModelListeners: addBookmarkModelListeners
  229.   };
  230. });
  231.